# listener.py
import os
import tkinter as tk
from PIL import Image, ImageTk
import time
import sys

class ListenerUI:
    def __init__(self, master, fullscreen=True):
        self.master = master
        master.title("监听程序")
        
        if fullscreen:
            master.attributes("-fullscreen", True)
        else:
            # 如果不是全屏，设置一个合适的窗口大小
            master.geometry("800x600")
            
        self.screen_width = master.winfo_screenwidth()
        self.screen_height = master.winfo_screenheight()

        master.configure(bg='#010101')  # 使用与主程序相同的透明色
        master.wm_attributes("-transparentcolor", "#010101")

        self.bg_canvas = tk.Canvas(self.master, width=self.screen_width, height=self.screen_height)
        self.bg_canvas.pack()

        # 绑定退出事件
        self.master.bind("<Escape>", lambda e: self.master.destroy())

        # 开始检查更新
        self.check_for_updates()

    def update_background(self, image_path):
        try:
            image = Image.open(image_path)
            image = image.resize((self.screen_width, self.screen_height), Image.LANCZOS)
            photo = ImageTk.PhotoImage(image)

            self.bg_canvas.delete("all")
            self.bg_canvas.create_image(0, 0, image=photo, anchor="nw")
            self.bg_canvas.image = photo  # 保持引用
        except Exception as e:
            print(f"更新背景失败: {str(e)}")

    def check_for_updates(self):
        if os.path.exists("background_update.txt"):
            with open("background_update.txt", "r") as f:
                new_background = f.read().strip()
            if new_background:
                self.update_background(new_background)
                os.remove("background_update.txt")
        self.master.after(100, self.check_for_updates)  # 每100ms检查一次

if __name__ == "__main__":
    root = tk.Tk()
    try:
        from ctypes import windll
        windll.shcore.SetProcessDpiAwareness(1)
    except:
        pass

    # 检查命令行参数
    fullscreen = "--fullscreen" in sys.argv
    
    app = ListenerUI(root, fullscreen=fullscreen)
    root.mainloop()